<template>
  <div class="box">
    <el-card style="margin-bottom:20px">
      <PageTools>
        <template #right>
          <el-button type="primary" size="mini" @click="addPermissions({ pid: '0', type: 1})">添加权限</el-button>
        </template>
      </PageTools>
    </el-card>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        prop="name"
        label="名称"
      />
      <el-table-column
        prop="code"
        label="标识"
      />
      <el-table-column
        prop="description"
        label="描述"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addPermissions({pid: scope.row.id, type: 2})">添加</el-button>
          <el-button type="text">编辑</el-button>
          <el-button type="text" @click="delPermissions(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框 -->
    <el-dialog title="收货地址" :visible.sync="dialogTableVisible">
      <Dialog
        v-if="dialogTableVisible"
        ref="dia"
        :dialog-table-visible.sync="dialogTableVisible"
        @success="success"
      />
    </el-dialog>
  </div>
</template>

<script>
import Dialog from './dialog.vue'
import { mapState } from 'vuex'

import { delPermission } from '@/api/permission'

export default {
  name: '',
  components: {
    Dialog
  },
  data() {
    return {
      // tableData: [],
      dialogTableVisible: false
    }
  },
  computed: {
    ...mapState('permissions', ['tableData'])
  },
  created() {
    this.$store.dispatch('permissions/getPermissionList')
  },
  methods: {
    // 获取数据=====================================================
    // async getPermissionList() {
    //   const res = await getPermissionList()
    //   this.tableData = transFormData(res.data, { id: 'id', pid: 'pid' })
    // },
    // 添加权限
    addPermissions(obj) {
      this.dialogTableVisible = true
      this.$nextTick(() => {
        this.$refs.dia.formData.pid = obj.pid
        this.$refs.dia.formData.type = obj.type
      })
    },
    // 删除
    async delPermissions(id) {
      const res = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (res === 'confirm') {
        await delPermission(id)
        this.$store.dispatch('permissions/getPermissionList')
      }
    },
    // 确定
    success() {
      this.dialogTableVisible = false
      this.$store.dispatch('permissions/getPermissionList')
    }
  }
}
</script>
<style lang='scss'  scoped>
.box {
  padding: 20px 30px;
}
</style>
